local function lobby_shop_priv(player)
   local player_attributes = player:get_meta()
   local xp = player_attributes:get_float('xp')
   local luck = player_attributes:get_int('luck')
   local health = player_attributes:get_int('hp')
   local formspec =
   'formspec_version[3]'..
   'size[12,12]'..
   'bgcolor[;neither]'..
   'background[-1,-1;14,14;lobby_shop.png]'..
   'textarea[.5,.5;11,2;;;   Spend your XP here! If you run low collect trash from the lobby, or play some levels with friends. '..
   'Playing levels will revoke some privileges, but you can restore them here. If you want to build your own levels you\'ll need the builder priv. '..
   'This will give you: fly, fast, and creative\n   Luck will help you find stashed items. Higher HP will let you take more damage before dying.]'..
   'textarea[1,3.75;5,1.5;;;Your Current Stats:\n XP: '..xp..'\n Max HP: '..health..'\n Luck: '..luck..']'..
   'button[1,7;5,1;builder;Builder Priv (100XP)]'..
   'button[6,7;5,1;toggle_build;Toggle builder privileges]'..
   'button[6,8.5;5,1;health;Increase your HP ('..((health+1)*10)..' XP)]'
   if luck < 10 then
      formspec = formspec..
      'button[1,8.5;5,1;luck;Increase your luck ('..((luck+1)*100)..' XP)]'
   else
      formspec = formspec..
      'button[1,8.5;5,1;nil;Max luck ]'
   end
   return formspec
end

local col_box = {
   type = 'fixed',
   fixed = {{-.5, -.5, -.45, 1.5, .375, 0},
            {-.45, 1.5, -.2, 1.45, 2, -.1},}--Left, Bottom, Front, Right, Top, Back
   }

minetest.register_node('lobby:shop_priv', {
   description = 'A place to buy privs',
   drawtype = 'mesh',
   mesh = 'lobby_shop_privs.obj',
   tiles = {'lobby_shop_privs.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = col_box,
   collision_box = col_box,
   on_rightclick = function(pos, node, clicker, itemstack)
      local meta = minetest.get_meta(pos)
      local name = clicker:get_player_name()
      meta:set_string('infotext', 'Shop, spend XP here.')
      minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(clicker))
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'lobby:shop_priv' then
      local player_attributes = player:get_meta()
      if fields.builder then
         if lobby.take_xp(player, 100) and not lobby.is_builder(player) then
            minetest.chat_send_player(player:get_player_name(), 'You just bought the Builder Privilege!')
            local privs = minetest.get_player_privs(name)
            privs.builder = true
            privs.creative = true
            privs.fly = true
            privs.fast = true
            minetest.set_player_privs(name, privs)
            minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
            player_attributes:set_string('mode', 'builder')
         else
            minetest.chat_send_player(name, 'You either don\'t have 100 XP or you already have the priv.')
         end
         minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
      elseif fields.toggle_build then
         if lobby.is_builder(player) then
            local privs = minetest.get_player_privs(name)
            if privs.creative == true then
               lobby.builder_to_player(player)
               minetest.chat_send_player(name, 'Privs revoked! Play some games in the lobby')
            else
               lobby.player_to_builder(player)
               minetest.chat_send_player(name, 'Privs restored!')
            end
         else
            minetest.chat_send_player(name, 'You need to buy the Builder priv first!')
         end
         minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
      elseif fields.luck then
         local luck = player_attributes:get_int('luck') or 0
         local xp = (luck+1)*100
         if lobby.take_xp(player, xp) then
            player_attributes:set_int('luck', luck+1)
         else
            minetest.chat_send_player(name, 'You need more XP!')
         end
         minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
      elseif fields.health then
         local health = player_attributes:get_int('hp')
         local xp = (health+1)*10
         if lobby.take_xp(player, xp) then
            player:set_properties({hp_max = health+1})
            player_attributes:set_int('hp', health+1)
         else
            minetest.chat_send_player(name, 'You need more XP!')
         end
         minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
      end
   end
end)
